package me.cxlr.qinlauncher2.view.launcher;

import android.content.Context;

import androidx.fragment.app.FragmentActivity;

import me.cxlr.qinlauncher2.constants.Constants;
import me.cxlr.qinlauncher2.dao.KeyActionDao;
import me.cxlr.qinlauncher2.manager.ActionManager;
import me.cxlr.qinlauncher2.manager.SoftwareManager;
import me.cxlr.qinlauncher2.model.KeyAction;
import me.cxlr.qinlauncher2.util.SharedPreferencesUtil;

/**
 * @author 酷安用户:山轮纸
 */
public class DpadKey {
    private volatile static DpadKey dpadKey;

    private DpadKey() {
        keyActionDao = new KeyActionDao();
    }

    public static DpadKey getInstance() {
        if (dpadKey == null) {
            synchronized (DpadKey.class) {
                if (dpadKey == null) {
                    dpadKey = new DpadKey();
                }
            }
        }
        return dpadKey;
    }

    private KeyActionDao keyActionDao;

    /**
     * DPad键功能
     *
     * @param keyName 键
     */
    public boolean dpadKey(FragmentActivity activity, Context context, String keyName) {
        boolean flag = SharedPreferencesUtil.getInstance().getBoolean("use_dpad_key", false);
        if (flag) {
            KeyAction keyAction = keyActionDao.findKeyActionByKeyNameAndKeyType(keyName,Constants.KEY_TYPE_DPAD);
            switch (keyAction.getActionType()) {
                case Constants.KEY_ACTION_TYPE_OPEN_APP:
                    SoftwareManager.getInstance().openApplication(activity, context, keyAction.getApp().getPackageName(), keyAction.getApp().getClazzName());
                    break;
                case Constants.KEY_ACTION_TYPE_ACTION:
                    ActionManager.getInstance().openAction(activity, context, keyAction.getAction().getActionName());
                    break;
                default:
                    break;
            }
            return true;
        } else {
            return false;
        }
    }
}
